<?php 
namespace App\Http\Controllers\Home;
use Session;
use App\User;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Home\ThrottlesLogins;
use Illuminate\Foundation\Home\AuthenticatesAndRegistersUsers;
use DB;

class LoginController extends Controller{

	public function login(Request $req){
			if(!empty($_POST)){
			   $this->validate($req , [
                    'user_name'=>'required',
                    'user_password'=>'required'
                ],[
                    'user_name.required'=>'账号不能为空!!',
                    'user_password.required'=>'密码不能不填写!!'
                ]
            );
			$login = new \App\login();
			$res = $login->login($_POST['user_name']);
			if($res == NULL){
				return "<script>alert('用户不存在，请重新输入，或去注册用户！！');location.href='login'</script>";
			}
			if($res->user_password!==md5($_POST["user_password"])){
				return "<script>alert('密码不正确，请重新输入！！');location.href='login'</script>";
			}
			Session::set('user_name',$res->user_name);
			Session::set('user_id',$res->user_id);
			$data = ['login_num'=>$res->login_num+1,'last_login_times'=>time()];
			$ints = $login->login_up($data,$res->user_id);
			if($ints==0){
				return "<script>alert('记录不正确！！');location.href='login'</script>";
			}
			echo  "<script>alert('登录成功！！');location.href='http://www.nbshop.com'</script>";
		}
		return view('Home/login');
	}

	//退出
	public function logout(){
		Session::flush();

		return "<script>alert('退出成功！！');location.href='http://www.nbshop.com'</script>";

	}



	//用户注册
	public function regist(Request $req){

         if($_POST){

         	 //自动验证
            $this->validate($req,[
                'user_name' => [
	                'required',
	                'regex:/^[^x00-xff]|\D{6,8}$/',
                ],
                'user_password' => [
	                'required',
	                'regex:/^[0-9]{3,6}$/',
                ],
	             'user_email' => [
	                'required',
	                'regex:/^\w+@\w+(\.)(com|cn|edn|gov)$/',
                ],
                'user_tel' => [
	                'required',
	                'regex:/^13\d{9}|15\d{9}|18\d{9}$/',
                ],
              
            ],
                [
                    'user_name.required' => '用户名不能为空！',
                    'user_name.regex' => '用户名格式不对，应汉字或6到8位字母！',
                    'user_password.required' => '密码不能为空！',
                    'user_password.regex' => '密码必须是3-6位的数字',
                    'user_email.required' => '邮箱不能为空！',
                    'user_email.regex' => '邮箱格式不对 应***@***.(com|cn|edn|gov)！',
                    'user_tel.required' => '手机不能为空！',
                    'user_tel.regex' => '手机格式不对 应13，15，18开头的11位数字！',
                ]
            );
             //post 数组
         	 $arr=array(
                'user_name'=>$_POST['user_name'],
                'user_password'=>md5($_POST['user_password']),
                'user_email'=>$_POST['user_email'],
                'user_tel'=>$_POST['user_tel'],
                'create_time'=>time(),
                'user_photo'=>'/Home/images/258PICJ58PICRr3_1024.jpg'
            );
            
            //判断两次密码一致吗
            if($_POST['user_password']==$_POST['user_passwrds']){
               $res = new \App\Login();
            
	           $data = $res->add($arr);
	           if($data!=0){
	           	 return "<script>alert('注册成功！');location.href='login'</script>";
	           }else{
	           	 return "<script>alert('注册失败！');location.href='regist'</script>";
	           }
            }else{
                 return "<script>alert('两次密码不一致！');location.href='regist'</script>";
            }
           
         }

         return view('Home/regist');   
	
     }


     //忘记密码
     public function forget(){
        if(empty($_POST)){
            return view('Home/forget'); 
        }else{
            $message = new \App\Message_Home();
            $list=$message->lists($_POST);
           if($list->code == $_POST['code']){
                if(time() - $list->addtime >5*60){
                    echo '验证码过期';
                }else{
                  return redirect('home/change');
                }  
           }else{
                echo '<script>alert("验证码错误");window.location.href="forget"</script>';
           }
        }
     }

     //短信验证发送
     public function yanzhengma($user_name,$user_tel){
           $code = rand(0000,9999);
           $res= DB::table('message')->insert(
                [
                'user_name'=>$user_name,
                'user_tel'=>$user_tel,
                'addtime'=>time(),
                'code'=>$code
                ]
                );
           if($res){
                $target = "http://sms.106jiekou.com/utf8/sms.aspx";
                $post_data = "account=dy249065839&password=dy980616&mobile=".$user_tel."&content=".rawurlencode("您的验证码是：".$code."。如需帮助请联系客服。");
                echo $gets = $this->Post($post_data, $target);
           }

     }
      function Post($data, $target) {
         $url_info = parse_url($target);
         $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
         $httpheader .= "Host:" . $url_info['host'] . "\r\n";
         $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
         $httpheader .= "Content-Length:" . strlen($data) . "\r\n";
         $httpheader .= "Connection:close\r\n\r\n";
         //$httpheader .= "Connection:Keep-Alive\r\n\r\n";
         $httpheader .= $data;


         $fd = fsockopen($url_info['host'], 80);
         fwrite($fd, $httpheader);
         $gets = "";
         while(!feof($fd)) {
             $gets .= fread($fd, 128);
         }
         fclose($fd);
         return $gets;
     }

     public function change(){
        if(empty($_POST)){
           return view('Home/change');    
        }else{
            if($_POST['user_password'] != $_POST['user_password_two']){
                echo '<script>alert("两次密码不一致");window.location.href="change"</script>';
            }else{
                $message = new \App\Message_Home();
                $res=$message->changepwd($_POST);
                if($res){
                    return redirect('home/index');
                }else{
                    echo '<script>alert("密码修改失败，稍后再试");window.location.href="change"</script>';
                }
            }
        }
     }


}


 ?>